//获取应用实例
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    toastHidden: true, //吐司  
    toastText: '',//吐司文本  
    list: [],
    touch_start: '',
    touch_end: '',
    hidden: true,
    OPEN_ID:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var OPEN_ID_ = wx.getStorageSync('OPEN_ID');
    console.log(OPEN_ID_)
    this.setData({
      OPEN_ID: OPEN_ID_
    });
    if (this.data.OPEN_ID != '') {
      var self = this;
      getData(self);
    }
  },


  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();
    if (this.data.OPEN_ID != '') {
      var self = this;
      getData(self);
    }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },
  toAddAlbum:function(event){
    wx.navigateTo({
      url: '../addAlbum/addAlbum'
    })
  },
  toDetil: function (event) {
    let that = this;
    console.log(event);
    // 取得下标
    var index = parseInt(event.currentTarget.dataset.index);
    // 取出id值
    var AlbumGuid = that.data.list[index].AlbumGuid;
    wx.navigateTo({
      url: "../album/album?AlbumGuid=" + AlbumGuid
    })
  },
  toModDetil: function (event) {
    let that = this;
    console.log(event);
    // 取得下标
    var index = parseInt(event.currentTarget.dataset.index);
    // 取出id值
    var Album = that.data.list[index];
    console.log(Album);
    var OPEN_ID_ = wx.getStorageSync('OPEN_ID');
    if (Album.OwnerUserGuid == OPEN_ID_){
      wx.navigateTo({
        url: "../addAlbum/addAlbum?Album=" + JSON.stringify(Album)
      })
    }else{
      wx.showToast({
        title: '无权修改！',
        image: '../../images/img_jinggao.png',
        duration: 1000
      })
    }
    
  },
  // 删除相册
  deleteAibum(event) {
    let that = this;
    // this.setData({ showActionsSheet: true, imageInAction: event.target.dataset.src });
    console.log(event)
    var position = event.currentTarget.dataset.index;
    var OPEN_ID_ = wx.getStorageSync('OPEN_ID');
    console.log(position)
    if (that.data.list[position].OwnerUserGuid == OPEN_ID_) {
        wx.showModal({
          title: '提示',
          content: '是否删除该相册？',
          success: function (res) {
            if (res.confirm) {
              deleteCategory(that.data.list[position].AlbumGuid, that, OPEN_ID_);
            }
          }
        })
    } else {
      wx.showToast({
        title: '无权删除！',
        image: '../../images/img_jinggao.png',
        duration: 1000
      })
    }
    
  },
})

//获取数据
var getData = function (self) {
  wx.request({
    url: app.globalData.ServiceUrl + 'album/getAllAlbumByOwnerUserGuid', //接口地址
    method: 'POST',
    data: {
      ValidateData: app.globalData.ValidateData,
      paras: {
        OwnerUserGuid: self.data.OPEN_ID,
        index: 1,
        currentPageCount: 100
      }
    },
    header: {
      'content-type': 'application/json' // 默认值
    },
    success: function (res) {
      console.log(res.data)
      if (res.data.BusinessInfo.Code == 1 && res.data.ReturnInfo.Code == 1) {
        if (res.data.UserArea.list.length == 0) {
          self.setData({
            hidden: false,
            list: res.data.UserArea.list
          });
        } else {
          self.setData({
            hidden: true,
            list: res.data.UserArea.list
          });
        }
      } else {
        wx.showToast({
          title: '获取数据失败！',
          image: '../../images/img_jinggao.png',
          duration: 1000,
        })
      }
    }
  })
}

//删除相册
var deleteCategory = function (AlbumGuid, self, OPEN_ID_) {
  console.log(AlbumGuid)
  wx.request({
    url: app.globalData.ServiceUrl + 'album/DeleteAlbum', //接口地址
    method: 'POST',
    data: {
      ValidateData: app.globalData.ValidateData,
      paras: {
        AlbumGuids: AlbumGuid,
        OwnerUserGuid: OPEN_ID_
      }
    },
    header: {
      'content-type': 'application/json' // 默认值
    },
    success: function (res) {
      console.log(res.data)
      if (res.data.BusinessInfo.Code == 1 && res.data.ReturnInfo.Code == 1) {
        wx.showToast({
          title: '删除相册成功！',
          icon: 'success',
          duration: 1000,
          complete: function () {
            //删除成功，重新加载
            getData(self);
          } //接口调用结束的回调函数  
        })

      } else {
        wx.showToast({
          title: '删除相册失败！',
          image: '../../images/img_jinggao.png',
          duration: 1000
        })
      }
    }
  })
}